/*
 * Publisher.java
 *
 * Created on May 12, 2006, 2:45 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author jtma
 */

import java.sql.SQLException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.regex.*;
import java.util.StringTokenizer;


public class Publisher {
    
    private String _outputfile;
    
    /** Creates a new instance of Publisher */
    public Publisher(String filename) {
        _outputfile = filename;
    }

    private void dump(String str)
    {
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(_outputfile, true));
            pw.println(str);
            pw.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

    /*
    public void publishEvent(Database db, String group, String gender, String event)
    {
        db.getAthletes(group, gender, event, _outputfile);
    }
    
    private void rowify(Vector v)
    {
        int i = 0;
        dump("<tr>");
        for (i = 0; i < v.size(); i++) {
            String p = "&nbsp;";
            if (v.get(i) != null) {
                p = (String)v.get(i);
            }
            dump("<td border=1px>" + p + "</td>");
        }
        dump("</tr>");
    }
    
    
    public void publishOrder(Database db)
    {
      // Barehand
        new File(_outputfile).delete();
        dump("(Saturday 9:30 am -- 1:00 pm)");
        dump("");
        
        publishEvent(db, "Group B", "Female", "Changquan");
        publishEvent(db, "Group B", "Female", "Nanquan");

        publishEvent(db, "Group B", "Male"  , "Changquan");
        publishEvent(db, "Group B", "Male"  , "Nanquan");


        publishEvent(db, "Group A", "Female", "Changquan");
        publishEvent(db, "Group A", "Female", "Taijiquan");

        publishEvent(db, "Group A", "Male"  , "Changquan");
        publishEvent(db, "Group A", "Male"  , "Nanquan");
        publishEvent(db, "Group A", "Male"  , "Taijiquan");


      // Short Weapon
        dump("(Saturday 1:30 pm -- 5:00 pm)\n");
        dump("");
                publishEvent(db, "Group B", "Female", "Broadsword");
        publishEvent(db, "Group B", "Female", "Straightsword");

        publishEvent(db, "Group B", "Male"  , "Broadsword");
        publishEvent(db, "Group B", "Male"  , "Straightsword");


        publishEvent(db, "Group A", "Female", "Broadsword");
        publishEvent(db, "Group A", "Female", "Straightsword");
        publishEvent(db, "Group A", "Female", "Taiji Sword");

        publishEvent(db, "Group A", "Male"  , "Broadsword");
        publishEvent(db, "Group A", "Male"  , "Straightsword");
        publishEvent(db, "Group A", "Male"  , "Southern Broadsword");
        publishEvent(db, "Group A", "Male"  , "Taiji Sword");

      // Long Weapon
        dump("(Sunday 9:00 am -- 11:00 am)\n");
                publishEvent(db, "Group B", "Female", "Staff");

        publishEvent(db, "Group B", "Male"  , "Staff");


        publishEvent(db, "Group A", "Female", "Staff");
        publishEvent(db, "Group A", "Female", "Spear");

        publishEvent(db, "Group A", "Male"  , "Staff");
        publishEvent(db, "Group A", "Male"  , "Spear");
        publishEvent(db, "Group A", "Male"  , "Southern Staff");

    }
    
    public void publish(Database db)
    {
        // print an agglomerated athlete order list for each event
        publishOrder(db);
        
        //print table for judges to fill in
        publishJudgingOrder(db);
        
        // print detailed separate files for each individual event
        
        // print a file for overall standings
    }
    
    public void publishEvent2(Database db, String ageGroup, String group, String gender, String event)
    {
        Vector list = db.getAthletes(ageGroup, group, gender, event);
        int i;
        dump("<h3>" + group + " " + gender + " " + event + " </h3>");
        dump("<table width=100% border=\"1\">");
        Vector items = new Vector();
        items.add("Order");
        items.add("Name");
        items.add("Mistakes");
        items.add("A Score");
        items.add("B Score");
        items.add("Other");
        items.add("Final");
        dump("<strong>");
        rowify(items);
        dump("</strong>");


        for (i = 0; i < list.size(); i++) 
        {
            items.clear();
            StringTokenizer t = new StringTokenizer((String) list.get(i));
            //items.add("Order");
            items.add(String.format("%d", i+1));
            //items.add("Name");
            items.add(t.nextToken() + " " + t.nextToken());
            //items.add("Mistakes");
            items.add("&nbsp;");
            //items.add("A Score");
            items.add("&nbsp;");
            //items.add("B Score");
            items.add("&nbsp;");
            //items.add("Other");
            items.add("&nbsp;");
            //items.add("Final");
            items.add("&nbsp;");
            rowify(items);
        }
        dump("</table>");
        dump("<br>");
    }
    public void publishJudgingOrder(Database db)
    {
      // Barehand
        new File(_outputfile).delete();
        dump("<html>");
        //dump("(Saturday 9:30 am -- 1:00 pm)");
        //dump("");
        
        publishEvent2(db, "Group B", "Female", "Changquan");
        publishEvent2(db, "Group B", "Female", "Nanquan");

        publishEvent2(db, "Group B", "Male"  , "Changquan");
        publishEvent2(db, "Group B", "Male"  , "Nanquan");


        publishEvent2(db, "Group A", "Female", "Changquan");
        publishEvent2(db, "Group A", "Female", "Taijiquan");

        publishEvent2(db, "Group A", "Male"  , "Changquan");
        publishEvent2(db, "Group A", "Male"  , "Nanquan");
        publishEvent2(db, "Group A", "Male"  , "Taijiquan");


      // Short Weapon
        //dump("(Saturday 1:30 pm -- 5:00 pm)\n");
        //dump("");
        publishEvent2(db, "Group B", "Female", "Broadsword");
        publishEvent2(db, "Group B", "Female", "Straightsword");

        publishEvent2(db, "Group B", "Male"  , "Broadsword");
        publishEvent2(db, "Group B", "Male"  , "Straightsword");


        publishEvent2(db, "Group A", "Female", "Broadsword");
        publishEvent2(db, "Group A", "Female", "Straightsword");
        publishEvent2(db, "Group A", "Female", "Taiji Sword");

        publishEvent2(db, "Group A", "Male"  , "Broadsword");
        publishEvent2(db, "Group A", "Male"  , "Straightsword");
        publishEvent2(db, "Group A", "Male"  , "Southern Broadsword");
        publishEvent2(db, "Group A", "Male"  , "Taiji Sword");

      // Long Weapon
        //dump("(Sunday 9:00 am -- 11:00 am)\n");
        publishEvent2(db, "Group B", "Female", "Staff");

        publishEvent2(db, "Group B", "Male"  , "Staff");


        publishEvent2(db, "Group A", "Female", "Staff");
        publishEvent2(db, "Group A", "Female", "Spear");

        publishEvent2(db, "Group A", "Male"  , "Staff");
        publishEvent2(db, "Group A", "Male"  , "Spear");
        publishEvent2(db, "Group A", "Male"  , "Southern Staff");        
        
        dump("</html>");
    }
    public void publishEvent3(Database db, String group, String gender, String event)
    {
        
        try {
            Vector list = db.getAthletesFull(group, gender, event, _outputfile);
            int i;
            dump("<h3>" + group + " " + gender + " " + event + " </h3>");
            dump("<table width=100% border=\"1\">");
            Vector items = new Vector();
            items.add("Order");
            items.add("Name");
            items.add("Mistakes");
            items.add("A Score");
            items.add("B Score");
            items.add("Other");
            items.add("Reason");
            items.add("Time");
            items.add("Final");
            dump("<strong>");
            rowify(items);
            dump("</strong>");
            

            for (i = 0; i < list.size(); i++) {
                rowify((Vector) list.get(i));
            }
            dump("</table>");
            dump("<br>");
        }
        catch (SQLException e) {
            e.printStackTrace();
            System.exit(1);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
         
    }
    */
    
    public void publishScores(Database db)
    {
        //temporary store diff time
        long tempTime=globalData.diffTime;
            
      // Barehand
        //String tempOutputFile = _outputfile;
        new File(_outputfile).delete();

        dump("<html>");
            
            dump("<head>");
                dump("<title>");
                    dump("Final Report");
                dump("</title>");
            dump("</head>");

            dump("<body>");
                dump("<pre>");
                
                //print report
                dump(db.getReport());

//                //print athlete order
//                dump(db.getAthleteOrder());
//                
//                //print athlete nandu
//                dump(db.getAthletesNandu());
        
                dump("</pre>");
           dump("</body>");
               
        dump("</html>");
        
        _outputfile = "..\\data\\EventScores.html";
         new File(_outputfile).delete();

        dump("<html>");
            
            dump("<head>");
                dump("<title>");
                    dump("Final Report");
                dump("</title>");
            dump("</head>");

            dump("<body>");
                dump("<pre>");
                
                //print report
                dump(db.getScore());

        
                dump("</pre>");
           dump("</body>");
               
        dump("</html>");
        
        _outputfile = "..\\data\\AthleteOrder.html";
         new File(_outputfile).delete();

        dump("<html>");
            
            dump("<head>");
                dump("<title>");
                    dump("Final Report");
                dump("</title>");
            dump("</head>");

            dump("<body>");
                dump("<pre>");
                
                //print athlete order
                dump(db.getAthleteOrder());
        
                dump("</pre>");
           dump("</body>");
               
        dump("</html>");
//        
//        _outputfile = "..\\data\\AthletesNandu.html";
//        new File(_outputfile).delete();
//
//        dump("<html>");
//            
//            dump("<head>");
//                dump("<title>");
//                    dump("Final Report");
//                dump("</title>");
//            dump("</head>");
//
//            dump("<body>");
//                dump("<pre>");
//                
//                //print athlete nandu
//                dump(db.getAthletesNandu());
//        
//                dump("</pre>");
//           dump("</body>");
//               
//        dump("</html>"); 
        //restore diffTime
        globalData.diffTime=tempTime;
    }
}
